import { axios } from '@/utils/request'

/**
 * 下载文件 用于excel导出
 * @param url
 * @param parameter
 */
export function downFiles(url,parameter){
  return axios({
    url: url,
    data: parameter,
    method:'post',
    responseType: 'blob',

  })
}

/**
 * 下载文件 用于excel导出
 * @param exportObj
 */
export function handleExportXlsNew(exportObj){
  if(!exportObj.name || typeof exportObj.name != "string"){
    exportObj.name = "导出文件"
  }
  let file = ''
  if(exportObj.extension){
    file = exportObj.name+exportObj.extension
  } else {
    file = exportObj.name+'.xls'
  }
  // extension

  // console.log("导出参数",params)
  downFiles(exportObj.url,exportObj.data).then((data)=>{
    if (!data) {
      this.$message.warning("文件下载失败")
      return
    }
    // console.log('文件下载失败===>',data);
    if (typeof window.navigator.msSaveBlob !== 'undefined') {
      window.navigator.msSaveBlob(new Blob([data],{type: 'application/vnd.ms-excel'}), file)
    }else{
      let url = window.URL.createObjectURL(new Blob([data],{type: 'application/vnd.ms-excel'}))
      let link = document.createElement('a')
      link.style.display = 'none'
      link.href = url
      link.setAttribute('download', file)
      document.body.appendChild(link)
      link.click()
      document.body.removeChild(link); //下载完成移除元素
      window.URL.revokeObjectURL(url); //释放掉blob对象
    }
  })
}

